<template>
    <div class="pagination">
        <button :disabled="pageNo == 1" @click="getPageNo(pageNo - 1)">
            上一页
        </button>
        <button v-if="startNumEndNum.start > 1" @click="getPageNo(1)" :class="{ active: pageNo == 1 }">
            1
        </button>
        <button v-if="startNumEndNum.start > 2">···</button>

        <button v-for="(page, index) in startNumEndNum.end" :key="index" v-if="page >= startNumEndNum.start"
            @click="getPageNo(page)" :class="{ active: pageNo == page }">
            {{ page }}
        </button>

        <button v-if="startNumEndNum.end < totalPage - 1">···</button>
        <button v-if="startNumEndNum.end < totalPage" @click="getPageNo(totalPage)"
            :class="{ active: pageNo == totalPage }">
            {{ totalPage }}
        </button>
        <button :disabled="pageNo == totalPage" @click="getPageNo(pageNo + 1)">
            下一页
        </button>

        <button style="margin-left: 30px">共 {{ total }} 条</button>
        <!-- <h1>{{ startNumEndNum }}=={{ pageNo }}</h1> -->
    </div>
</template>

<script>
export default {
    name: "Pagination",
    props: ["pageNo", "pageSize", "total", "continues"],
    //pageNo: 当前第几个
    //pagesize: 代表每一页展示多少条数据
    //total: 代表整个分页一共要展示多少条数据
    //continues: 代表分页连续页码个数
    //计算属性
    computed: {
        //计算出一共多少页
        totalPage() {
            //向上取整
            return Math.ceil(this.total / this.pageSize);
        },
        //计算出连续的页码的起始数字和结束数字[连续页码数最少是5]
        startNumEndNum() {
            //先定义两个变量存储其实和结束的变量
            let start = 0,
                end = 0;
            if (this.continues > this.totalPage) {
                //当连续页码数大于实际页码数
                start = 1;
                end = this.totalPage;
            } else {
                //当连续页码数小于于实际页码数
                // 34 5 67
                //345 6 789
                //1 2 345
                // start = this.pageNo - Math.floor(this.continues / 2)
                start = this.pageNo - parseInt(this.continues / 2);
                end = this.pageNo + parseInt(this.continues / 2);
                //当处于 第 1 ，2 页时候 start会变成0或者负数
                if (start < 1) {
                    start = 1;
                    end = this.continues;
                }
                if (end > this.totalPage) {
                    end = this.totalPage;
                    start = this.totalPage - (this.continues - 1);
                }
            }
            return { start, end };
        },
    },
    methods: {
        getPageNo(page) {
            this.$bus.$emit("getPageNo", page);
        },
    },
};
</script>

<style lang="less" scoped>
.pagination {
    text-align: center;

    button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
            color: #c0c4cc;
            cursor: not-allowed;
        }

        &.active {
            cursor: not-allowed;
            background-color: #ff4040;
            color: #fff;
        }
    }
}
</style>
